﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace PJOnimal
{
    public class Character
    {
        public Sprite mySprite = new Sprite();//animation cho nhan vat
        public int jump = 0;//0: dung yen 1:dang nhay 2:dang roi
        float yjump = 0;//vi tri nhay
        string preKey = "";//key truoc        
        int tocDoDiChuyen = 2; //tốc độ di chuyển
        int tocDoNhay = 4;// tốc độ nhảy
        int highJump = 0;//do cao nhay duoc
        float maplength = 7200;//chieu dai toi da map
        public Vector2 toaDoNhanVat; //tọa độ nhân vật (cần lấy để thực thi matrix)
        public Vector2 toaDoCu= new Vector2 (100,455);
        string str;//path nhan vat
        public float toaDoDung = 610;//toa do nhan vat co the dung va di chuyen
        float maxJump = 0;//toa do toi day se roi
        //chieu cao nhan vat
        float wnhanVat = 100;//'52;
        //chieu rong nhan vat
        float hnhanVat = 100;//76;
        float[] hwDu = {24,24,16,6};//pixel du theo trai,phai,tren,duoi
        float wgach = 50;//chieu rong gach
        float hgach = 50;//chieu cao gach
        //flag toi lui hay nhay
        public string flagToi = "";//kiem tra toi
        public string flagLui = "";//kiem tra lui
        int flagCheckTren = 0;
        int flagRoi = 0;     
        public Character(Onimal game)
        {
            //load hinh
            switch (game.NhanVatSelect)
            {
                case 0:
                    str = "Character/Char2/";
                    break;
                case 1:
                    str = "Character/Char3/";
                    break;
                case 2:
                    str = "Character/Char4/";
                    break;
                case 3:
                    str = "Character/Char5/";
                    break;
                case 4:
                    str = "Character/Char1/";
                    break;
                default:
                    str = "Character/Char1/";
                    break;
            }

            mySprite.NTexture2d = 1;
            Texture2D[] texture2d;
            texture2d = new Texture2D[16];
            //load character
            for (int i = 0; i < 16; i++)
            {
                texture2d[i] = game.Content.Load<Texture2D>(str + "0" + i);
            }
            mySprite = new Sprite(100, 0, texture2d, 8, -1, 0);
            mySprite.Height = 100;
            mySprite.Width = 100;
            mySprite.DHeightO = 16;
            mySprite.DHeightU = 6;
            mySprite.DWidthL = 24;
            mySprite.DWidthR = 24;
            //set highJump
            highJump = tocDoNhay * 50;
            maxJump = 455 - highJump;
        }
        public void Move(GameTime gameTime, MapGame mapGame,float xCamera)
        {
            if (mySprite.FlagAction == 11)
            {
                mySprite.UpdateDiChuyen(0, +tocDoNhay);
                return;
            }
            //if(jump ==0)
            //    GiamBang(toaDoDung, 0, 1);
            VaCham(mapGame, toaDoCu);
            if (mySprite.X < -xCamera - 10)
                flagLui = "lui";
            else
                flagLui = "";
            KeyboardState kb;
            kb = Keyboard.GetState();

            if (kb.IsKeyDown(Keys.S) || kb.IsKeyDown(Keys.Down))
            {

            }
            //nhay
            if ((kb.IsKeyDown(Keys.W) || kb.IsKeyDown(Keys.Up)) && jump == 0)
            {
                jump = 1;
                yjump = toaDoDung ;
                maxJump = toaDoDung - highJump ;
                if (mySprite.FlagAction < 3)
                    mySprite.FlagAction = 2;
                else
                    mySprite.FlagAction = 6;
                
                if(jump ==0)
                    mySprite.UpdateDiChuyen(0, -tocDoNhay);
                
                preKey = Keys.W.ToString();
            }

            //lui
            if (kb.IsKeyDown(Keys.A) || kb.IsKeyDown(Keys.Left))
            {                
                if (preKey == "D")
                    mySprite.FlagAction = 4;
                else
                    mySprite.FlagAction = 5;
                
                if (mySprite.X > 0 && flagLui != "lui")
                {
                    flagToi = "";
                    mySprite.UpdateDiChuyen(-tocDoDiChuyen, 0);                    
                    if (jump != 0)
                        mySprite.UpdateDiChuyen(-tocDoDiChuyen, 0);
                    
                }
                
                mySprite.UpdateAnimation(gameTime);
                preKey = Keys.A.ToString();
            }
            //toi
            else if (kb.IsKeyDown(Keys.D) || kb.IsKeyDown(Keys.Right))
            {
                if (preKey == "A")
                    mySprite.FlagAction = 0;
                else
                    mySprite.FlagAction = 1;
                
                if (mySprite.X < maplength && flagToi != "toi")
                {
                    flagToi = "";
                    mySprite.UpdateDiChuyen(tocDoDiChuyen, 0);
                    if (jump != 0)
                        mySprite.UpdateDiChuyen(tocDoDiChuyen, 0);
                }
                
                mySprite.UpdateAnimation(gameTime);
                preKey = Keys.D.ToString();
            }
            else
            {
                if (mySprite.FlagAction < 3)
                {
                    mySprite.FlagAction = 0;
                    mySprite.UpdateAnimation(gameTime);
                }
                else
                {
                    mySprite.FlagAction = 4;
                    mySprite.UpdateAnimation(gameTime);
                }
            }
            //nhay len
            if (jump == 1 && (mySprite.Y - tocDoNhay) >= maxJump )
            {

                mySprite.UpdateDiChuyen(0, -tocDoNhay);
                
                mySprite.UpdateAnimation(gameTime);
                if (mySprite.Y == yjump - highJump)
                {
                    jump = 2;
                    //return;
                }
            }
            else if((mySprite.Y - tocDoNhay) <= maxJump )
            {
                jump = 2;
            }
            //roi xuong
            if (jump == 2)
            {
                mySprite.UpdateDiChuyen(0, +tocDoNhay);
                
                mySprite.UpdateAnimation(gameTime);
                if (mySprite.Y + tocDoNhay > toaDoDung)
                {                    
                    jump = 0;
                    //set lai toa do dung                    
                    if (mySprite.Y > toaDoDung )
                    {
                        //mySprite.Y = 456;
                        toaDoDung = 610;
                    }
                    if (mySprite.FlagAction < 3)
                    {
                        mySprite.FlagAction = 0;
                        mySprite.UpdateAnimation(gameTime);
                    }
                    else
                    {
                        mySprite.FlagAction = 4;
                        mySprite.UpdateAnimation(gameTime);
                    }
                    

                }
            }
            
            toaDoCu = new Vector2(mySprite.X, mySprite.Y);
        }
        
        public void VaCham(MapGame mapGame, Vector2 toaDoCu)
        {
            flagRoi=0;            
            for (int i = 0; i < mapGame.lstObj.Count; i++)
            {
                Vector2 mapObj = new Vector2(((MapObj)mapGame.lstObj[i]).X, ((MapObj)mapGame.lstObj[i]).Y);
                if (mySprite.X - mapObj.X > 100 || mapObj.X - mySprite.X > 100)
                    continue;
                ////neu toa do Y ma khi X tang co the cham vat
                //if (mySprite.Y + hnhanVat - hwDu[3]> mapObj.Y && mySprite.Y +mySprite.DHeightO< mapObj.Y + hgach)
                //{
                //    //Neu toa do X hon vat
                //    if ((mySprite.X + wnhanVat - hwDu[1] + tocDoDiChuyen) > mapObj.X && mySprite.X + hwDu[0] < mapObj.X)
                //    {
                //        flagToi = "toi";
                //    }                    
                //    else
                //    {                       
                //        flagLui = "";
                //        //Neu toa do X nho hon vat
                //        if ((mySprite.X + hwDu[0] - tocDoDiChuyen) < (mapObj.X + wgach) && mySprite.X + wnhanVat - hwDu[1] > mapObj.X + wgach)
                //        {
                //            flagLui = "lui";
                //        }
                //        else
                //            flagToi = "";
                //    }
                //}//Neu toa do Y o ngoai vat và X hon vat
                //else if (mySprite.X + wnhanVat - hwDu[1] > mapObj.X && mySprite.X + hwDu[0] < mapObj.X + wgach)
                //{
                //    //Neu vat o duoi
                //    if ((mySprite.Y + hnhanVat-hwDu [3] + tocDoNhay) > mapObj.Y && mySprite.Y + hwDu [2]< mapObj.Y)
                //    {
                //        jump = 0;
                //        toaDoDung = mySprite.Y;
                //    }//Neu vat o tren
                //    else if ((mySprite.Y + hwDu [2]- tocDoNhay) < (mapObj.Y + hgach) && mySprite.Y -hwDu [3] + hnhanVat > mapObj.Y)
                //    {
                //        jump = 2;
                //    }
                //    //kiem tra nhan co o tren gach khong
                //    flagRoi = 1;
                //    //kiem tra nhan vat da tung o tren gach chua
                //    flagCheckTren = 1;
                //}//Nguoc lai o ngoai x va y
                //else
                //{
                //    flagToi = "";
                //    flagLui = "";
                //    //neu vat o ngoai x va y
                //    if (mySprite.X + wnhanVat - hwDu[1] < mapObj.X && mySprite.Y + hnhanVat + hwDu[0] < mapObj.Y || mySprite.X > mapObj.X + wgach && mySprite.Y + hnhanVat < mapObj.Y)
                //    {
                //        if (jump == 0)
                //        {
                //            jump = 2;
                //        }
                //    }
                //}
                DungCham(mapObj, new Vector2(((MapObj)mapGame.lstObj[i]).Width, ((MapObj)mapGame.lstObj[i]).Height  ),0);
            }
            //Neu
            if (flagRoi == 0 && flagCheckTren == 1)
            {
                if (jump == 0)
                {
                    jump = 2;
                }
                flagCheckTren = 0;
                toaDoDung = 610;
            }
        }
        public int DungCham(Vector2 mapObj,Vector2 heightWidth,int flagDie)
        {
            int flagdie = 0;
            if (flagDie == 11)
                return -1;
             //neu toa do Y ma khi X tang co the cham vat
            if (mySprite.Y + mySprite.Height - mySprite.DHeightO > mapObj.Y && mySprite.Y + mySprite.DWidthR < mapObj.Y + heightWidth.Y)
                {
                    //Neu toa do X hon vat
                    if ((mySprite.X + mySprite.Width - mySprite.DWidthR + tocDoDiChuyen) > mapObj.X && mySprite.X + mySprite.DWidthL < mapObj.X)
                    {
                        flagToi = "toi";
                        flagdie = 1;
                    }                    
                    else
                    {                       
                        flagLui = "";
                        //Neu toa do X nho hon vat
                        if ((mySprite.X + mySprite.DWidthL - tocDoDiChuyen) < (mapObj.X + heightWidth.X) && mySprite.X + mySprite.Width - mySprite.DWidthR > mapObj.X + heightWidth.X)
                        {
                            flagLui = "lui";
                            flagdie = 1;
                        }
                        else
                            flagToi = "";
                    }
                }//Neu toa do Y o ngoai vat và X hon vat
                else if (mySprite.X + mySprite.Width - mySprite.DWidthR > mapObj.X && mySprite.X + mySprite.DWidthL < mapObj.X + heightWidth.X)
                {
                    //Neu vat o duoi
                    if ((mySprite.Y + mySprite.Height-mySprite.DHeightU + tocDoNhay) > mapObj.Y && mySprite.Y + mySprite.DHeightO< mapObj.Y)
                    {
                        jump = 0;
                        toaDoDung = mySprite.Y;
                        flagdie = 2;
                    }//Neu vat o tren
                    else if ((mySprite.Y + mySprite.DHeightO- tocDoNhay) < (mapObj.Y + heightWidth.Y) && mySprite.Y -mySprite.DHeightU + mySprite.Height > mapObj.Y)
                    {
                        flagdie = 1;
                        jump = 2;
                    }
                    //kiem tra nhan co o tren gach khong
                    flagRoi = 1;
                    //kiem tra nhan vat da tung o tren gach chua
                    flagCheckTren = 1;
                }//Nguoc lai o ngoai x va y
                else
                {
                    flagToi = "";
                    flagLui = "";
                    //neu vat o ngoai x va y
                    if (mySprite.X + mySprite.Width - mySprite.DWidthR < mapObj.X && mySprite.Y + mySprite.Height + mySprite.DWidthL < mapObj.Y || mySprite.X > mapObj.X + heightWidth.X && mySprite.Y + mySprite.Height < mapObj.Y)
                    {
                        if (jump == 0)
                        {
                            flagdie = 1;
                            jump = 2;
                        }
                    }
                }              
            
            return flagdie;
        }
        //chua su dung
        public void GiamBang(float toaDo,float heSoX,float heSoY)
        {
            while (mySprite.Y != toaDo)
            {
                mySprite.UpdateDiChuyen(heSoX, heSoY);
            }
        }
        public void UpdateToaDo(float x,float y)
        {
            mySprite.UpdateToaDo(x,y);
        }
        public void Update(GameTime gameTime, MapGame mapGame, float xCamera)
        {
            tocDoDiChuyen = 2;
            tocDoNhay = 4;            
            toaDoNhanVat = new Vector2(mySprite.X, mySprite.Y);
            Move(gameTime, mapGame, xCamera);
            
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            mySprite.Draw(spriteBatch);
        }
    }
}
